package cn.itlaobing.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import cn.itlaobing.entity.Student;
import cn.itlaobing.entity.enums.Gender;
import cn.itlaobing.service.StudentService;
@Controller("/students/update")
public class StudentUpdate extends BaseController{

	Log log = LogFactory.getLog(StudentUpdate.class);
	@Autowired
	private StudentService studentService;
	
	//获取表单
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		//获取对象
		Student student=studentService.findById(Long.valueOf(id));
		request.setAttribute("student", student);
		request.setAttribute("genders", Gender.values());
		toView("/students/update.jsp", request, response);	
	}

	// 更新
	@Override
		public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String gender=request.getParameter("gender");
			String birthday=request.getParameter("birthday");
			
			studentService.update(Long.valueOf(id), name, gender, birthday);
			redirectTo("/students/home",request,response);

		}
}
